package francis.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class WebTools {

	/**
	 * 通过图片url返回图片Bitmap
	 * 通过图片url返回图片Bitmap的工具方法"(如果不需要异步，直接调用这个方法)
	 * http://shazhuzhu1.iteye.com/blog/937680
	 * @param url
	 * @return
	 */
	public static Bitmap returnBitMap(String path) {
		URL url = null;
		Bitmap bitmap = null;
		try {
			url = new URL(path);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 利用HttpURLConnection对象,我们可以从网络中获取网页数据.
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream(); // 得到网络返回的输入流
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	/**
	 * 获取网页信息
	 */
	public static Document getWebContent(String path) {
		
		Document document = null ;
		
		try {
			SAXReader reader = new SAXReader();
			document = reader.read(path);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return document;
	}
}
